import tkinter as tk
import numpy as np
from collections import defaultdict

# 学生体检表数据
data = [
    [170, 160, 150],  # 身高
    [18, 17, 16],  # 年龄
    [70, 65, 60]  # 体重
]

# 剔除重复的数组元素
def remove_duplicates():
    unique_elements = list(set(data[0]))
    print(unique_elements)

# 计算协方差矩阵的特征值和特征向量
def pca(data):
    # 返回特征数（数组的长度）
    rows = len(data)
    # 返回样本数量，取第一行即可
    cols = len(data[0])
    # 返回平均数数组
    vector = [sum(row) / cols for row in data]
    # 返回方差数组
    matrix = []
    for index, row in enumerate(data):
        sum_val = 0
        for num in row:
            diff = num - vector[index]
            sum_val += diff ** 2
        # 保存为字典方便排序
        matrix.append({'num': sum_val / cols, 'data': row})
    # 开始排序
    matrix.sort(key=lambda x: x['num'], reverse=True)
    return matrix

def run_pca():
    # 测试数据
    result = pca(data)
    print(result)

# 创建主窗口
root = tk.Tk()
root.title("PCA 计算")

# 创建按钮
remove_duplicates_button = tk.Button(root, text="剔除重复元素", command=remove_duplicates)
remove_duplicates_button.pack(pady=10)

pca_button = tk.Button(root, text="运行 PCA 计算", command=run_pca)
pca_button.pack(pady=10)

# 运行主循环
root.mainloop()